﻿using Coomtools.commclass;
using CYLScheck.UIcomm;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CYLScheck.UIForms
{
    public partial class PageSettingFrm : Sunny.UI.UIPage
    {






        /// <summary>
        /// 接受主界面传来的 关闭主界面方法的委托；
        /// </summary>
        private Declosedmainform Colse_mainFrm;
      /// <summary>
      /// 设置页面窗体的构造函数
      /// </summary>
      /// <param name="closeMFrm">关闭主窗体方法的委托类型</param>
        public PageSettingFrm(Declosedmainform closeMFrm )
           
        {
            InitializeComponent();
             Colse_mainFrm = closeMFrm;
        }






       /// <summary>
       /// 将读取到的参数赋值给对应的控件值
       /// </summary>
        void Getini_toTextbox()
        {
            uTextbox_servername.Text = Gobleset.link_servername.Trim();
            
            uTextbox_databasename.Text = Gobleset.link_databasename.Trim();
            
            uTextbox_databaseuser.Text = Gobleset.link_datauser.Trim();
            uTextbox_databasepassword.Text = Gobleset.link_datapassword.Trim();
            uTextbox_databaseport.Text = Gobleset.link_serverport.Trim();
            uiTBox_username.Text = Gobleset.username;
            uiTBox_password.Text = "";
            uiTBox_password.Text = Gobleset.userpassword;
           //设置是否启用登录窗体的 控件状态
            if (Gobleset.enableuser == true)
            {
                uiSwitch_enableuser.Active = true;
            }
            else uiSwitch_enableuser.Active = false;

        }

       bool Writesetini()
        {
            ///用 sunnyui 的inifileex 工具类 写入  loginset.int 文件初始配置信息
            IniFileEx iniwrite = new IniFileEx("loginset.ini");
            try
            {
            iniwrite.Write("link", "servername", uTextbox_servername.Text.Trim());
           
            iniwrite.Write("link", "database", uTextbox_databasename.Text.Trim());
            iniwrite.Write("link", "serverport", uTextbox_databaseport.Text.Trim());
            iniwrite.Write("link", "datauser", uTextbox_databaseuser.Text.Trim());
            iniwrite.Write("link", "datapassword", uTextbox_databasepassword.Text.Trim());

                iniwrite.Write("user", "username", uiTBox_username.Text.Trim());
                iniwrite.Write("user", "userpassword",new MyEncrypt().EncryptString(uiTBox_password.Text.Trim()) );
                // 写入 false 或true
                if (uiSwitch_enableuser.Active)
                {
                    iniwrite.Write("user", "enableuser", true);

                }
                else
                {
                    iniwrite.Write("user", "enableuser", false);
                }

                return true;
                     
            }
            catch (Exception)
            {
                UIMessageBox.ShowError("写入配置文件 loginset.ini 失败！ 请检查文件是否存在！");
                
                 return false;
                throw;
                  
            }
           
       


        }
        private void PageSettingFrm_Load(object sender, EventArgs e)
        {
            loadsetting.load_iniset();
            Getini_toTextbox();
            if (Gobleset.Gobel_IsRegUser=="NO")
            {
                uiSwitch_enableuser.Enabled = false;
            }
            this.Style =Gobleset.Gobel_userstyle ;
           
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }



        private void uiSButton_ok_Click(object sender, EventArgs e)
        {
            //如果写入失败就报错
            if ( Writesetini()==false)
            {
                return;
            }
            if (UIMessageDialog.ShowAskDialog(new Form(),"参数设置成功！是否重启程序！"))
            {
                Colse_mainFrm();
            }
            

        }

        private void PageSettingFrm_Initialize(object sender, EventArgs e)
        {
            //uiFlowLayoutPanel1.FlowDirection=
        }

        private void uiPanel2_Click(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void uiDesigerFrm1_Click(object sender, EventArgs e)
        {
            DesigerForm desigerFrm = new DesigerForm();
            desigerFrm.ShowDialog();
        }

        private void button_WeightShow_Click(object sender, EventArgs e)
        {
           
            WegihtFrm mywegithform=new WegihtFrm();
            mywegithform.ShowDialog();
     
        }
    }
}
